package com.example.treehole.util;

import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import com.example.treehole.PostDetailActivity;

public class BlueTextUtil {
    public static class ClickableBlueSpan extends ClickableSpan {
        private final String mText;
        private final String mFullText;
        private final View.OnClickListener mListener;

        public ClickableBlueSpan(String text,String fullText, View.OnClickListener listener) {
            mText = text;
            mFullText = fullText;
            mListener = listener;
        }

        @Override
        public void onClick(View widget) {
            mListener.onClick(widget);
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(Color.parseColor("#3AD0F9"));
            ds.setUnderlineText(false);
        }
    }

    public static void setBlueText(TextView textView, String text, String fullText, View.OnClickListener listener) {
        SpannableStringBuilder builder = new SpannableStringBuilder(fullText);
        ClickableBlueSpan clickableSpan = new ClickableBlueSpan(text, fullText, listener);
        builder.setSpan(clickableSpan, 2, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(builder);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }
}